// Andrew Gooch
// Asymmetric Partisan and Ideological Evaluations from Candidate Repositioning: A Randomized Experiment


// This replication file includes:
// 1) import CVS from Qualtrics and save as DTA (already ran and commented out)
// 2) Loaded DTA version of data, drop non-consent resondents, auxiliary variables from Qualtrics, and recode variables for main analysis
// 3) Recodes variables
// 4) Analysis from main document (plots) and appendix (full regression tables). Note balance tables are in a different do file.


//Instructions:
// To run code, change directory location first to match your terminal (Data and Results folders)
// Each step above is label (STEP ONE, STEP TWO, etc.)
// All code can be highlighted and run at once after changing directory paths.


// STEP ONE

// Original CSV is availabe in replication file
// Only change made to CVS file was deleting the redundent top row of question labels from Qualtrics
// import CVS and turn into DTA
// Step one will be commented out after first running so that the final DTA dataset can be used with code
// Note the code to create the Stata log is commented out

/*
clear all
set more off

cd "C:\Users\Andrew Gooch\Dropbox\Published\13 Asymmetric Responses to Repositioning\ReplicationArchive\Data"

import delimited using Repositioning_data

// drop unused and auxiliary Qualtrics variables
drop responseid startdate enddate status ipaddress progress durationinseconds finished rid recordeddate recipientlastname recipientfirstname recipientemail externalreference locationlatitude locationlongitude distributionchannel userlanguage region 

// drop competency outcome measure & three treatment outcome measures used in different publication

save Repositioning, replace */

// STEP TWO

// load DTA file 
clear all
set more off

cd "C:\Users\Andrew Gooch\Dropbox\Published\13 Asymmetric Responses to Repositioning\ReplicationArchive\Data"
//log using mylogA, replace
use Repositioning

// DROP THOSE WHO DO NOT CONSENT AND THOSE WHO FAIL ATTENTION CHECK

// drop people who did not consent
drop if q0 == "I do not agree to participate"

// drop people who failed the attention check
drop if q00 != "Cat" // 43 people failed

// STEP THREE

// VARIABLE RECODES:

// create treatment var
gen treatment = .
replace treatment = 1 if q6_1 != .
replace treatment = 2 if q9_1 != .
replace treatment = 3 if q13_1 != .
label define treatment 1 "Campaign Position" 2 "Consistent" 3 "Reposition" 
label values treatment treatment 
label var treatment "Treatment assignment"

//Dummy variables for regression models
gen treatment_1 = 1 if treatment == 1 // consistent in office
replace treatment_1 = 0 if treatment !=1
label var treatment_1 "Campaign"

gen treatment_2 = 1 if treatment == 2 // consistent in office
replace treatment_2 = 0 if treatment !=2
label var treatment_2 "Consistent"

gen treatment_3 = 1 if treatment == 3 // repositioning in office
replace treatment_3 = 0 if treatment !=3
label var treatment_3 "Reposition"

//Remove three treatments were published in a different article
drop if treatment == .

//Favorability, combine each treatment question into one var
//combine outcome measures
egen favorability = rowmax(q6_1 q9_1 q13_1) 
label var favorability "Favorability 0-100"

//Turn ideology question into 0-1 scale, then combine each to make a single var
//t1
gen ideo_1 = .
replace ideo_1 = 1 if q8 == "Very Liberal"
replace ideo_1 = .75 if q8 == "Liberal"
replace ideo_1 = .5 if q8 == "Moderate"
replace ideo_1 = .25 if q8 == "Conservative"
replace ideo_1 = 0 if q8 == "Very Conservative"
replace ideo_1 = 99 if q8 == "Not sure"
//t2
gen ideo_2 = .
replace ideo_2 = 1 if q11 == "Very Liberal"
replace ideo_2 = .75 if q11 == "Liberal"
replace ideo_2 = .5 if q11 == "Moderate"
replace ideo_2 = .25 if q11 == "Conservative"
replace ideo_2 = 0 if q11 == "Very Conservative"
replace ideo_2 = 99 if q11 == "Not sure"
//t3
gen ideo_3 = .
replace ideo_3 = 1 if q15 == "Very Liberal"
replace ideo_3 = .75 if q15 == "Liberal"
replace ideo_3 = .5 if q15 == "Moderate"
replace ideo_3 = .25 if q15 == "Conservative"
replace ideo_3 = 0 if q15 == "Very Conservative"
replace ideo_3 = 99 if q15 == "Not sure"

//combined perceived candidate ideo var
egen ideology_01 = rowmax(ideo_1 ideo_2 ideo_3) // 129 skipped or broke off before treatment
//remove DK people from ideology var, recode to missing
replace ideology_01 = . if ideology == 99
summ ideology_01 // from 0-1
//recode 0-100 so that it is on the same scale as favorability
gen ideology = ideology_01*100
summ ideology
label var ideology "Ideology 0-100 (100=Very Lib, 50 = Mod, 0 = Very Con)"

//variable for those who cannot place ideologically
gen ideo_DK = .
replace ideo_DK = 1 if ideology == .
replace ideo_DK = 0 if ideology != .


//PRE-TREATMENT COVARIATES
// PRE-REGISTERED SUBGROUP ANALYSIS: 
// 1) position on asylum seekers,
// 2) party identification, 
// 3) self-placed ideology, 

// position on asylum seekers
gen asylum_position = .
replace asylum_position = 1 if q5 =="Increase the number of asylum seekers per year"
replace asylum_position = 0 if q5 =="Maintain the current amount of 15,000 asylum seekers per year"
replace asylum_position = -1 if q5 =="Reduce the number of asylum seekers per year"
label var asylum_position "1=Increase, 0=Maintain, -1=Reduce"
  

// party ID
//see codebook from Lucid
gen pid = .
replace pid = -1 if political_party == 1 | political_party == 2 | political_party == 3 | political_party ==6
replace pid = 0 if political_party == 4 | political_party == 7
replace pid = 1 if political_party == 5 | political_party == 8 | political_party == 9 | political_party==10

// Self-placed Ideology
gen self_ideology = .
replace self_ideology = -2 if q1 =="Very Liberal"
replace self_ideology = -1 if q1 =="Liberal"
replace self_ideology = 0 if q1 == "Moderate" | q1=="Not sure"
replace self_ideology = 1 if q1 =="Conservative"
replace self_ideology = 2 if q1 =="Very Conservative"

// DEMOGRAPHIC CONTROL VARS FOR REGRESSION

// insert means for missing demographics
//first create var without missing to get means
gen education_no_miss = education
replace education_no_miss = . if education_no_miss == -3105
summ education_no_miss // mean = 4.498
replace education = 4.498 if education == -3105
replace education = 4.498 if education == 9

gen hhi_no_miss = hhi
replace hhi_no_miss = . if hhi_no_miss == -3105
summ hhi_no_miss // mean = 4.498
replace hhi = 9.489 if hhi == -3105

// race
gen white = 0
replace white = 1 if ethnicity == 1

// race
gen black = 0
replace black = 1 if ethnicity == 2

//male
gen male = .
replace male = 1 if gender == 1
replace male = 0 if gender == 2

//Subgroup indicators for CIplot function of means and confidence intervals (These will be used in Fig 4 & 6)
gen ideo_camp_dem = .
replace ideo_camp_dem = ideology if treatment ==1 & pid == -1
label var ideo_camp_dem "Democrats"

gen ideo_camp_rep = .
replace ideo_camp_rep = ideology if treatment ==1 & pid == 1
label var ideo_camp_rep "Republicans"

gen ideo_repo_dem = .
replace ideo_repo_dem = ideology if treatment ==3 & pid == -1
label var ideo_repo_dem "Democrats"

gen ideo_repo_rep = .
replace ideo_repo_rep = ideology if treatment ==3 & pid == 1
label var ideo_repo_rep "Republicans"

gen ideo_camp_decr = .
replace ideo_camp_decr = ideology if treatment ==1 & asylum_position == -1
label var ideo_camp_decr "Conservatives"

gen ideo_camp_incr = .
replace ideo_camp_incr = ideology if treatment ==1 & asylum_position == 1
label var ideo_camp_incr "Liberals"

gen ideo_repo_decr = .
replace ideo_repo_decr = ideology if treatment ==3 & asylum_position == -1
label var ideo_repo_decr "Conservatives"

gen ideo_repo_incr = .
replace ideo_repo_incr = ideology if treatment ==3 & asylum_position == 1
label var ideo_repo_incr "Liberals"

// STEP FOUR

//RESULTS!!

//Change directory to results folder
cd "C:\Users\Andrew Gooch\Dropbox\Published\13 Asymmetric Responses to Repositioning\ReplicationArchive\Results"

//FIGURE 1: Overall Treatment Effects

//FAVORABILITY
xi: reg favorability treatment_2 treatment_3 hhi education white black male
est store Favorability
outreg2 [Favorability] using "Favorability_Model1.xls", replace ctitle(Favorability) bracket

//ideology 
xi: reg ideology treatment_2 treatment_3 hhi education white black male
est store Ideology
outreg2 [Ideology] using "Ideology_Model1.xls", replace ctitle(Ideology) bracket

coefplot (Favorability, offset(-0.05) msymbol(S)) (Ideology, offset(0.05) msymbol(Oh)),  ///
drop(_cons hhi education white black male) xline(0) ///
yline(0, lpattern(dash)) ytitle("Changes (%) Relative to Campaign Position") ///
graphregion(color(white)) vertical legend(rows(1)) legend(position(1) label(1 Favorability) label(2 Ideology)) note(" ")   


//FIGURE 2: Favorability by Proximity

//Conservative position (less immigrant)
xi: reg favorability treatment_2 treatment_3 hhi education white black male if asylum_position == -1
est store Conservative
outreg2 [Conservative] using "Favorability_Model2.xls", replace ctitle(Favor Conservative) bracket

//Status quo position (same number of immigrant)
xi: reg favorability treatment_2 treatment_3 hhi education white black male if asylum_position == 0
est store Maintain
outreg2 [Maintain] using "Favorability_Model3.xls", replace ctitle(Favor Same) bracket

//Liberal position (more immigrant)
xi: reg favorability treatment_2 treatment_3 hhi education white black male if asylum_position == 1
est store Liberal
outreg2 [Liberal] using "Favorability_Model4.xls", replace ctitle(Favor Liberal) bracket

coefplot (Conservative, offset(-0.05) msymbol(S)) (Maintain, offset(0.00) msymbol(Oh))  (Liberal, offset(0.05) msymbol(Th)),  ///
drop(_cons hhi education white black male) xline(0) ///
yline(0, lpattern(dash)) ytitle("Changes (%) Relative to Campaign Position") ///
graphregion(color(white)) vertical legend(rows(1)) legend(position(1) label(1 Conservative) label(2 Maintain) label(3 Liberal)) note(" ")   


//FIGURE 3: Ideology by Proximity

//Conservative position (less immigrant)
xi: reg ideology treatment_2 treatment_3 hhi education white black male if asylum_position == -1
est store Conservative
outreg2 [Conservative] using "Ideology_Model2.xls", replace ctitle(Ideology Conservative) bracket

//Status quo position (same number of immigrant)
xi: reg ideology treatment_2 treatment_3 hhi education white black male if asylum_position == 0
est store Maintain
outreg2 [Maintain] using "Ideology_Model3.xls", replace ctitle(Ideology Same) bracket

//Liberal position (more immigrant)
xi: reg ideology treatment_2 treatment_3 hhi education white black male if asylum_position == 1
est store Liberal
outreg2 [Liberal] using "Ideology_Model4.xls", replace ctitle(Ideology Liberal) bracket

coefplot (Conservative, offset(-0.05) msymbol(S)) (Maintain, offset(0.00) msymbol(Oh))  (Liberal, offset(0.05) msymbol(Th)),  ///
drop(_cons hhi education white black male) xline(0) ///
yline(0, lpattern(dash)) ytitle("Changes (%) Relative to Campaign Position") ///
graphregion(color(white)) vertical legend(rows(1)) legend(position(1) label(1 Conservative) label(2 Maintain) label(3 Liberal)) note(" ")   


// FIGURE 4: Average Ideology by Proximity, Campaign and Repositioning Treatment Only

// AVERAGES AND CIs OF IDEOLOGY BY ASYLUM SEEKER
ciplot ideo_camp_decr ideo_camp_incr ideo_repo_decr ideo_repo_incr ///
, graphregion(color(white)) inclusive ylabel (35(5) 100) note(" ") l1title("Ideology in Each Conditions (0-100)") 


//FIGURE 5: Ideology by Proximity

//Democrats only
xi: reg ideology treatment_2 treatment_3 hhi education white black male if pid == -1
est store Democrats
outreg2 [Democrats] using "Ideology_Model5.xls", replace ctitle(Ideology Democrats) bracket

//Pure independents only
xi: reg ideology treatment_2 treatment_3 hhi education white black male if pid == 0
est store Independents
outreg2 [Independents] using "Ideology_Model6.xls", replace ctitle(Ideology Independents) bracket

// Republicans only
xi: reg ideology treatment_2 treatment_3 hhi education white black male if pid == 1
est store Republicans
outreg2 [Republicans] using "Ideology_Model7.xls", replace ctitle(Ideology Republicans) bracket

coefplot (Democrats, offset(-0.05) msymbol(Th)) (Independents, offset(0.00) msymbol(Oh))  (Republicans, offset(0.05) msymbol(S)),  ///
drop(_cons hhi education white black male) xline(0) ///
yline(0, lpattern(dash)) ytitle("Changes (%) Relative to Campaign Position") ///
graphregion(color(white)) vertical legend(rows(1)) legend(position(1) label(1 Democrats) label(2 Independents) label(3 Republicans)) note(" ")   


// FIGURE 6: Average Ideology by Party ID, Campaign and Repositioning Treatment Only

// AVERAGES AND CIs OF IDEOLOGY BY PARTY, PARTISANS ONLY
ciplot ideo_camp_dem ideo_camp_rep ideo_repo_dem ideo_repo_rep ///
, graphregion(color(white)) inclusive ylabel (35(5) 100) note(" ") l1title("Ideology in Each Conditions (0-100)") 



/*
log close
translate "C:\Users\Andrew Gooch\Dropbox\Published\13 Asymmetric Responses to Repositioning\ReplicationArchive\Data\mylogA.smcl" "C:\Users\Andrew Gooch\Dropbox\Published\13 Asymmetric Responses to Repositioning\ReplicationArchive\Data\mylogA.pdf", replace
*/

